package com.example.myapplication;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.data.DatabaseHelper;
import ohos.data.dataability.DataAbilityUtils;
import ohos.data.rdb.*;
import ohos.data.resultset.ResultSet;
import ohos.data.dataability.DataAbilityPredicates;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.rpc.MessageParcel;
import ohos.utils.net.Uri;
import ohos.utils.PacMap;

import java.io.*;

public class UserDataAbility extends Ability {
    private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD001100, "Demo");
    private RdbStore rdbStore;
    StoreConfig config=StoreConfig.newDefaultConfig("userStore.db");
    RdbOpenCallback callback=new RdbOpenCallback() {
        @Override
        public void onCreate(RdbStore rdbStore) {
            //创建表
            rdbStore.executeSql("create table if not exists users1(" +
                    "userId integer primary key autoincrement, " +
                    "userName text not null, " +
                    "password text not null, " +
                    "userTel text, " +
                    "userAddr text)");
            // 插入新记录的SQL语句
            String insertSql = "INSERT INTO users1 (userId, userName, password, userTel, userAddr) "
                    + "VALUES (null, 'zzz', '123', '111111', '北京')";

// 执行插入操作
            rdbStore.executeSql(insertSql);
        }

        @Override
        public void onUpgrade(RdbStore rdbStore, int i, int i1) {

        }
    };

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        HiLog.info(LABEL_LOG, "UserDataAbility onStart");
        //初始化数据库的连接
        DatabaseHelper helper=new DatabaseHelper(this);
        rdbStore =helper.getRdbStore(config,1,callback);
    }

    public ResultSet query(Uri uri, String[] columns, DataAbilityPredicates predicates) {
        RdbPredicates rdbPredicates = DataAbilityUtils.createRdbPredicates(predicates,"users1");
        ResultSet rs= rdbStore.query(rdbPredicates,columns);
        return rs;
    }

    @Override
    public int insert(Uri uri, ValuesBucket value) {
//        HiLog.info(LABEL_LOG, "userDataAbility insert");
        String path=uri.getLastPath();
        int i=-1;
        if("users1".equalsIgnoreCase(path))
        {
            i =(int) rdbStore.insert("users1",value);
        }
        return -1;
    }


    @Override
    public int delete(Uri uri, DataAbilityPredicates predicates) {
        RdbPredicates rdbPredicates= DataAbilityUtils.createRdbPredicates(predicates,"users1");
        int i= rdbStore.delete(rdbPredicates);
        return i;
    }

    @Override
    public int update(Uri uri, ValuesBucket value, DataAbilityPredicates predicates) {
        RdbPredicates rdbPredicates= DataAbilityUtils.createRdbPredicates(predicates,"users1");
        int i= rdbStore.update(value,rdbPredicates);
        return i;
    }

    @Override
    public FileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
        File file = new File(uri.getDecodedPathList().get(0));
        if (mode == null || !"rw".equals(mode)) {
            file.setReadOnly();
        }
        FileInputStream fileIs = new FileInputStream(file);
        FileDescriptor fd = null;
        try {
            fd = fileIs.getFD();
        } catch (IOException e) {
            HiLog.info(LABEL_LOG, "failed to getFD");
        }
        return MessageParcel.dupFileDescriptor(fd);
    }

    @Override
    public String[] getFileTypes(Uri uri, String mimeTypeFilter) {
        return new String[0];
    }

    @Override
    public PacMap call(String method, String arg, PacMap extras) {
        return null;
    }

    @Override
    public String getType(Uri uri) {
        return null;
    }
}